//
//  AppDelegate.m
//  G05爱鲜蜂
//
//  Created by 源 何 on 16/10/14.
//  Copyright © 2016年 何源. All rights reserved.
//

#import "AppDelegate.h"
#import "HMMainViewController.h"
#import <AFNetworkActivityIndicatorManager.h>
#import "HMNewFeatureController.h"
#import "HMHomeViewController.h"
#import <UMSocialCore/UMSocialCore.h>
@interface AppDelegate ()
/// 通知监听对象
@property (nonatomic, strong) id observer;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self setupAFNetworking];
    
    // 1. 实例化 window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.window.backgroundColor = [UIColor whiteColor];
    

    // 2. 设置 window 的根控制器
    [self isNewVersion];
    
    // 3. 让 window 成为主窗口并可见
    [self.window makeKeyAndVisible];
    //友盟
    [[UMSocialManager defaultManager] openLog:YES];
    
    [[UMSocialManager defaultManager] setUmSocialAppkey:@"58047915e0f55a67c9002793"];
    //微博
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"3665384080"  appSecret:@"a563303620aea1f75b18c8a1009ffdfc" redirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    return YES;
}


#pragma mark
#pragma mark -- 判断新旧版本
- (void)isNewVersion{
    
    // 获取当前的版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
    // 获取旧版本号
    NSString *oldVersion = [[NSUserDefaults standardUserDefaults] objectForKey:kVersion];
    
    // 做判断
    if ([oldVersion isEqualToString:currentVersion]) {
        // 相等, 显示 tabBar
        
        [self ads];
        self.window.rootViewController = [[HMMainViewController alloc] init];
        
    } else {
        // 显示欢迎界面
        self.window.rootViewController = [[HMNewFeatureController alloc] init];
        
        // 显示完一次欢迎界面之后, 应该保存当前的版本号
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:kVersion];
        
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}
-(BOOL)application:(UIApplication *)app openURL:(nonnull NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(nonnull id)annotation{
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    return result;
}

#pragma mark
#pragma mark -- 设置 AFNetworking 小菊花
- (void)setupAFNetworking{
    
    // 启用状态栏指示器
    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    
    // 设置缓存
    [NSURLCache setSharedURLCache:[[NSURLCache alloc] initWithMemoryCapacity: 4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil]];
}

#pragma mark
#pragma mark -- 程序启动广告
- (void)ads{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:@"7" forKey:@"call"];
    
    [DSHTTPClient postUrlString:@"ad.json.php" withParam:param withSuccessBlock:^(id data) {
        NSDictionary *dict = data[@"data"];
        NSURL *imgUrl = dict[@"img_big_name"];
        
        UIImageView *adImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [adImageView sd_setImageWithURL:imgUrl];
        
        [[UIApplication sharedApplication].keyWindow addSubview:adImageView];
        
        [UIView animateWithDuration:2.0 animations:^{
            adImageView.transform = CGAffineTransformMakeScale(1.2, 1.2);
            adImageView.alpha = 0;
        } completion:^(BOOL finished) {
            [adImageView removeFromSuperview];
            
        }];
        
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
}

@end
